package com.example.imchat.controller.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.example.imchat.R;
import com.example.imchat.model.Model;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMGroupManager;
import com.hyphenate.chat.EMGroupOptions;
import com.hyphenate.exceptions.HyphenateException;

public class NewGroupActivity extends Activity {
    private EditText et_newgroup_name;
    private EditText et_newgroup_desc;
    private CheckBox cb_newgroup_public;
    private CheckBox cb_newgroup_invite;
    private Button bt_group_create;
    private String groupName;
    private String groupDesc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_group);

        initView();

        initListener();
    }

    private void initListener() {
        //创建按钮的点击
        bt_group_create.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(TextUtils.isEmpty( et_newgroup_name.getText().toString() ) || TextUtils.isEmpty(  et_newgroup_desc.getText().toString() )){
                    Toast.makeText( NewGroupActivity.this,"群名称或群介绍不能为空",Toast.LENGTH_SHORT ).show();;
                    return;
                }
                Intent intent = new Intent(NewGroupActivity.this, PickContactActivity.class);
                /*
                * 我们这里假设有A、B两个Activity，由A跳转到B，然后B返回到A。
                在这一过程中，我们在A启动B的过程中调用startActivityForResult（）方法来启动B，
                然后在B中调用Context.setResult（）方法来传递返回的Code,
                并在A中的onActivityResult（）方法中解析返回的结果，并做相应的逻辑操作。
                * */
                startActivityForResult(intent,1);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //成功获取到联系人
        if (resultCode == RESULT_OK){
                //创建群
            createGroup(data.getStringArrayExtra("members"));
;            }

    }
    //创建群
    private void createGroup(String[] members) {
        //群名
        groupName = et_newgroup_name.getText().toString();
        //群描述
        groupDesc = et_newgroup_desc.getText().toString();
        Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                //去服务器建群
                //参数一 群名 参数二 群描述 参数三  群成员 参数四 创建群的原因 参数五 参数设置
                EMGroupOptions options = new EMGroupOptions();

                //设置该群为 需要发送群邀请让对方同意 默认是需要同意的  如果不设置 会导致同意群邀请失败  逻辑上会有问题

                options.inviteNeedConfirm = true;
                options.maxUsers = 200;//群容量
                EMGroupManager.EMGroupStyle groupStyle = null;
                if (cb_newgroup_public.isChecked()){
                    if (cb_newgroup_invite.isChecked()){
                        groupStyle = EMGroupManager.EMGroupStyle.EMGroupStylePublicOpenJoin;
                    }else {
                        groupStyle = EMGroupManager.EMGroupStyle.EMGroupStylePublicJoinNeedApproval;
                    }
                }else {
                    if (cb_newgroup_invite.isChecked()){
                        groupStyle = EMGroupManager.EMGroupStyle.EMGroupStylePrivateMemberCanInvite;
                    }else {
                        groupStyle = EMGroupManager.EMGroupStyle.EMGroupStylePrivateOnlyOwnerInvite;
                    }

                }
                options.style = groupStyle;//创建群类型
                try {
                    EMClient.getInstance().groupManager().createGroup(groupName,groupDesc,members,"申请加入群聊",options);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(NewGroupActivity.this,groupName+" 已成功创建",Toast.LENGTH_SHORT).show();
                            finish();
                        }
                    });

                } catch (HyphenateException e) {
                    e.printStackTrace();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(NewGroupActivity.this,groupName+"创建群聊失败",Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        });
    }

    private void initView() {
          et_newgroup_name = findViewById(R.id.et_newgroup_name);
          et_newgroup_desc = findViewById(R.id.et_newgroup_desc);
          cb_newgroup_public = findViewById(R.id.cb_newgroup_public);
          cb_newgroup_invite = findViewById(R.id.cb_newgroup_invite);
          bt_group_create = findViewById(R.id.bt_group_create);
    }
}
